package cn.tedu._03vehicle.base.interceptors;

import cn.tedu._03vehicle.pojo.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入登录状态校验拦截器！！！！");
        // 获取Session对象
        HttpSession session = request.getSession();
        // 从Session对象中获取用户名
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 判断用户名是否为空
        if (userVO == null) {
            response.sendRedirect("/login.html");
            // 用户没有登录，不允许访问
            throw new RuntimeException("用户未登录！");
            // 跳转到登录页面
        }else {
            // 用户已经登录，允许访问
            log.debug("用户已经登录，允许访问");
            return true;
        }
    }

}
